﻿using System;
using System.Collections.Generic;
using System.Text;
using AgencyManagerSystem.Model;
using AgencyManagerSystem.Common;
using System.Linq;
using Microsoft.Extensions.DependencyInjection;
using Dapper;

namespace AgencyManagerSystem.DataFetcher
{
    public class UserFetcher : BaseFetcher<UserModel>
    {
        public UserFetcher() : base()
        {

        }
        public override void AddDefaultFilter(StringBuilder stringBuilder, DynamicParameters dynamicParameters)
        {
            if (userProfile == null)
                return;
            if (userProfile.Type == UserType.Department)
            {
                string keyStr = String.Format("@{0}_{1}", "Department", COUNTER++);
                stringBuilder.AppendFormat(" AND Department = {0} ", keyStr);
                dynamicParameters.Add(keyStr, userProfile.Department);
            }
            if (userProfile.Type == UserType.Person)
            {
                string keyStr = String.Format("@{0}_{1}", "Persion", COUNTER++);
                stringBuilder.AppendFormat(" AND AutoId = {0} ", keyStr);
                dynamicParameters.Add(keyStr, userProfile.UserId);
            }
            if (userProfile.Type == UserType.SiteOrSupplier)
            {
                string keyStr = String.Format("@{0}_{1}", "Self", COUNTER++);
                stringBuilder.AppendFormat("  AND AutoId = {0} ", keyStr);
                dynamicParameters.Add(keyStr, userProfile.UserId);
            }
        }
        //public UserModel GetByLoginId(int loginId)
        //{
        //    Condition[] conditions = new Condition[] { new Condition() { Key = "LoginId", Op = "=", Value = loginId } };
        //    var list = List(conditions);
        //    if (list.Count > 0)
        //        return list.First();
        //    else
        //        return null;
        //}
        public UserModel GetByCode(string code)
        {
            Condition[] conditions = new Condition[] { new Condition() { Key = "Code", Op = "=", Value = code } };
            var list = List(conditions);
            if (list.Count > 0)
                return list.First();
            else
                return null;
        }
        public IList<UserModel> ListByDepartment(int department)
        {
            Condition[] conditions = new Condition[] { new Condition() { Key = "Department", Op = "=", Value = department }};
            var list = List(conditions);
            return list;
        }
        public IList<UserModel> ListByRetail(int retail)
        {
            Condition[] conditions = new Condition[] { new Condition() { Key = "Retail", Op = "=", Value = retail } };
            var list = List(conditions);
            return list;
        }
        public IList<UserModel> ListBySuppiler(int suppiler)
        {
            Condition[] conditions = new Condition[] { new Condition() { Key = "Suppiler", Op = "=", Value = suppiler } };
            var list = List(conditions);
            return list;
        }
    }
}
